local header = [[
]]

local trinkets = {
	{ name = "Assurance",		token = "assurance_of_consequence",
		varies = {
			{ id = 104974, suffix = "LFR",	ilvl = 528, maxupgrade = 2, },
			{ id = 104725, suffix = "Flex",	ilvl = 540, maxupgrade = 2, },
			{ id = 102292, suffix = "N",	ilvl = 553, },
			{ id = 105223, suffix = "WF",	ilvl = 559, },
			{ id = 104476, suffix = "H",	ilvl = 566, },
			{ id = 105472, suffix = "HWF",	ilvl = 572, },
		},
	},
	{ name = "Haromm",		token = "haromms_talisman",
		varies = {
			{ id = 105029, suffix = "LFR",	ilvl = 528, maxupgrade = 2, },
			{ id = 104780, suffix = "Flex",	ilvl = 540, maxupgrade = 2, },
			{ id = 102301, suffix = "N",	ilvl = 553, },
			{ id = 105278, suffix = "WF",	ilvl = 559, },
			{ id = 104531, suffix = "H",	ilvl = 566, },
			{ id = 105527, suffix = "HWF",	ilvl = 572, },
		},
	},
	{ name = "Rampage",		token = "sigil_of_rampage",
		varies = {
			{ id = 105082, suffix = "LFR",	ilvl = 528, maxupgrade = 2, },
			{ id = 104833, suffix = "Flex",	ilvl = 540, maxupgrade = 2, },
			{ id = 102302, suffix = "N",	ilvl = 553, },
			{ id = 105331, suffix = "WF",	ilvl = 559, },
			{ id = 104584, suffix = "H",	ilvl = 566, },
			{ id = 105580, suffix = "HWF",	ilvl = 572, },
		},
	},
	{ name = "Ticking",		token = "ticking_ebon_detonator",
		varies = {
			{ id = 105114, suffix = "LFR",	ilvl = 528, maxupgrade = 2, },
			{ id = 104865, suffix = "Flex",	ilvl = 540, maxupgrade = 2, },
			{ id = 102311, suffix = "N",	ilvl = 553, },
			{ id = 105363, suffix = "WF",	ilvl = 559, },
			{ id = 104616, suffix = "H",	ilvl = 566, },
			{ id = 105612, suffix = "HWF",	ilvl = 572, },
		},
	},
	{ name = "Xuen",		token = "discipline_of_xuen",
		varies = {
			{ id = 103986, suffix = "U",	ilvl = 535, maxupgrade = 2, },
			-- { id = 103686, suffix = "N",	ilvl = 496, maxupgrade = 2, },
		},
	},
	-- { name = "Medallion",		token = "arrowflight_medallion",
	-- 	varies = {
	-- 		{ id = 93258, suffix = "",	ilvl = 496, maxupgrade = 2, },
	-- 	},
	-- },
	{ name = "Renatakis",		token = "renatakis_soul_charm", mod = "-%dexp_%dcrit",
		varies = {
			-- { id = 95625, suffix = "LFR",	ilvl = 502, maxupgrade = 2, },
			{ id = 94512, suffix = "N",		ilvl = 522, },
			{ id = 95997, suffix = "TF",	ilvl = 528, },
			{ id = 96369, suffix = "H",		ilvl = 535, },
			{ id = 96741, suffix = "HTF",	ilvl = 541, },
		},
	},
	{ name = "RenatakisSat",		token = "renatakis_soul_charm,reforge=exp_crit",
		varies = {
			-- { id = 95625, suffix = "LFR",	ilvl = 502, maxupgrade = 2, },
			{ id = 94512, suffix = "N",		ilvl = 522, },
			{ id = 95997, suffix = "TF",	ilvl = 528, },
			{ id = 96369, suffix = "H",		ilvl = 535, },
			{ id = 96741, suffix = "HTF",	ilvl = 541, },
		},
	},
	{ name = "Badjuju",			token = "bad_juju",				mod = "-%dmastery_%dcrit",
		varies = {
			-- { id = 95665, suffix = "LFR",	ilvl = 502, maxupgrade = 2, },
			{ id = 94523, suffix = "N",		ilvl = 522, },
			{ id = 96037, suffix = "TF",	ilvl = 528, },
			{ id = 96409, suffix = "H",		ilvl = 535, },
			{ id = 96781, suffix = "HTF",	ilvl = 541, },
		},
	},
	{ name = "Bloodlust",		token = "talisman_of_bloodlust",
		varies = {
			-- { id = 95748, suffix = "LFR",	ilvl = 502, maxupgrade = 2, },
			{ id = 94522, suffix = "N",		ilvl = 522, },
			{ id = 96120, suffix = "TF",	ilvl = 528, },
			{ id = 96492, suffix = "H",		ilvl = 535, },
			{ id = 96864, suffix = "HTF",	ilvl = 541, },
		},
	},
	{ name = "Reorigination",	token = "rune_of_reorigination",
		varies = {
			-- { id = 95802, suffix = "LFR",	ilvl = 502, maxupgrade = 2, },
			{ id = 94532, suffix = "N",		ilvl = 522, },
			{ id = 96174, suffix = "TF",	ilvl = 528, },
			{ id = 96546, suffix = "H",		ilvl = 535, },
			{ id = 96918, suffix = "HTF",	ilvl = 541, },
		},
	},
	{ name = "ShadoPan",		token = "vicious_talisman_of_the_shadopan_assault",	mod = "-%dhit_%dcrit",
		varies = {
			{ id = 94511, suffix = "",		ilvl = 522, maxupgrade = 2, },
		},
	},
	{ name = "ShadoPanSat",		token = "vicious_talisman_of_the_shadopan_assault,reforge=hit_crit",
		varies = {
			{ id = 94511, suffix = "",		ilvl = 522, maxupgrade = 2, },
		},
	},
	-- { name = "Terror",			token = "terror_in_the_mists",
	-- 	varies = {
	-- 		{ id = 86890, suffix = "LFR",	ilvl = 483, maxupgrade = 2, },
	-- 		{ id = 86332, suffix = "N",		ilvl = 496, },
	-- 		{ id = 87167, suffix = "H",		ilvl = 509, },
	-- 	},
	-- },
	-- { name = "Bottle",			token = "bottle_of_infinite_stars",	mod = "-%dmastery_%dhaste",
	-- 	varies = {
	-- 		{ id = 86791, suffix = "LFR",	ilvl = 476, maxupgrade = 2, },
	-- 		{ id = 86132, suffix = "N",		ilvl = 489, },
	-- 		{ id = 87057, suffix = "H",		ilvl = 502, },
	-- 	},
	-- },
}

local stats = setmetatable( {
	-- [489] = 1079,
	-- [497] = 1162,
	-- [502] = 1218,
	-- [510] = 1312,

	-- [522] = 1467,
	-- [526] = 1522,
	-- [528] = 1552,
	-- [530] = 1580,
	-- [532] = 1610,
	-- [535] = 1657,
	-- [536] = 1672,
	-- [539] = 1719,
	-- [541] = 1752,
	-- [543] = 1785,
	-- [545] = 1818,
	-- [549] = 1887,
	-- [551] = 1923,
}, { __index = function ( tbl, key )
		 -- y = 11.30404888 e9.321765499·10-3 x
		 return math.floor(11.30404888 * math.exp(9.321765499e-3 * key ))
	end,
} )

function trinket_name( name, ilvl, suffix )
	return name .. "_" .. ilvl-- .. suffix
end

function get_line( slot, trinket )
	if not trinket then
		return ""
	end
	local line = ("trinket%d=%s,id=%d,upgrade=%d"):format(
		slot,
		trinket.token,
		trinket.id,
		trinket.upgrade
	)
	if trinket.mod then
		local stat = stats[trinket.ilvl]
		if not stat then
			print( "Unknown stat for ilvl", trinket.ilvl )
			return ""
		end
		line = line .. ",enchant=" .. (trinket.mod):format(stat, stat)
	end
	return line .. "\n"
end

function write_copy( file, trinket1, trinket2 )
	if not trinket1 then return end
	if trinket2 then
		file:write( ("name=%s_%s\n"):format(trinket1.name, trinket2.name) )
	else
		file:write( ("name=%s\n"):format(trinket1.print_name) )
	end
	file:write( get_line( 1, trinket1 ) )
	file:write( get_line( 2, trinket2 ) )
	file:write( "\n" )
end

function parse_trinket( trinket, index, upgrade )
	print( "parse_trinket", trinket.name, index, upgrade )
	local index = (index == "max") and #trinket.varies or index
	local vary = trinket.varies[index]
	local upgrade = upgrade

	if not vary then
		return
	end
	if upgrade == "max" then
		upgrade = vary.maxupgrade or 4
	end
	if vary.maxupgrade and upgrade > vary.maxupgrade then
		return
	end

	local ilvl = vary.ilvl + upgrade * 4
	local t = {}
	for key, value in pairs( trinket ) do
		t[key] = value
	end
	t.print_name = trinket_name(trinket.name, ilvl, vary.suffix)
	t.ilvl = ilvl
	t.id = vary.id
	t.upgrade = upgrade
	return t
end

-- -- Generate trinket combinations
-- print( "Generating Combo Profiles" )
-- local file = io.open("Agility_Trinket_Combo.simc", "w")
-- file:write( header )
-- file:write( "html=Agility_Trinket_Combo.html\n\n" )

-- for i = 1, #trinkets do
-- 	local trinket1 = parse_trinket( trinkets[i], "max", "max" )
-- 	for j = i + 1, #trinkets do
-- 		local trinket2 = parse_trinket( trinkets[j], "max", "max" )
-- 		if trinket1 and trinket2 then
-- 			write_copy( file, trinket1, trinket2 )
-- 		else
-- 			print( "Skipped char: ", i, j )
-- 		end
-- 	end
-- end

-- file:close()

-- Generate trinket comparation
print( "Generating Compare Profiles" )
os.execute("mkdir gen")
os.execute("del gen\\* /Q")
local file_index = 1
for _, trinket in ipairs( trinkets ) do
	for index = 1, #trinket.varies do
		for upgrade = 0, 4, 2 do

			local trinket = parse_trinket( trinket, index, upgrade )
				if trinket then
				local file = io.open(("gen\\Compare_%d.simc"):format(file_index), "w")
				-- file:write( header )
				-- file:write( ("html=output\\Compare_%d.html\n\n"):format( file_index ) )
				-- file:write("html=Compare.html\n\n")
				write_copy( file, trinket )

				file:close()
				file_index = file_index + 1
			end
		end
	end
end
